package com.csun.nusing.ui.fragment;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.csun.nusing.R;
import com.csun.nusing.familypresenter.FragmentFamilyHomePresenter;
import com.csun.nusing.presenter.inter.IHome;
import com.csun.nusing.ui.familyactivity.FamilyAddOlderActivity;
import com.csun.nusing.ui.familyactivity.FamilyAlarmListActivity;
import com.csun.nusing.ui.familyactivity.FamilyAllDeviceActivity;
import com.csun.nusing.ui.familyactivity.FamilyHealthListActivity;
import com.csun.nusing.ui.familyactivity.FamilyOldrestDetailsActivity;
import com.csun.nusing.ui.myview.RoundImageView;
import com.csun.nusing.ui.myview.TabLayout;
import com.csun.nusing.util.SPUtils;
import com.csun.zxing.CaptureActivity;

/**
 * 家属端主界面 ClassName: FamilyHomeFragment
 *
 * @author fzq
 * @Description: TODO
 * @date 2017-11-6
 */
public class FamilyHomeFragment extends
        BaseFragment<FragmentFamilyHomePresenter> implements IHome {

    private LinearLayout devicell;
    private LinearLayout alarmll;
    private LinearLayout healthll;
    private LinearLayout oldsterll;
    private TextView bloodoxyTv;
    private TextView bloodpressTv;
    private TextView bloodsugerTv;
    private TextView heartrateTv;
    private TextView huxiTv;
    private TextView temperatureTv;
    private RoundImageView photoimageView;
    private TextView nameTv;
    private String oldId;
    private String oldpicId;
    private String oldName;
    private TabLayout mTabLayout;
    public String olderPhotopath;
    public String olderBir;
    public String olderSex;
    public String olderEcgname;
    public String olderEcgPhone;
    private String olderage;
    private String roomBed;
    private String roomId;
    private String bedNo;
    private ImageView adddevice;
    private ImageView addolder;

    public FamilyHomeFragment() {
    }

    public static FamilyHomeFragment newInstance() {
        return new FamilyHomeFragment();
    }

    @Override
    public int getContentViewId() {
        return R.layout.fragment_family_home;
    }

    // findView
    @Override
    public void initSubViews(View view) {
        super.initSubViews(view);
        findView(view);
        controlView();
    }

    private void controlView() {
    }

    /**
     * 控件
     *
     * @param @param view
     * @return void
     * @throws
     * @Description: TODO
     * @author fzq
     * @date 2017-10-20
     */
    private void findView(View view) {
        mTabLayout = view
                .findViewById(R.id.fragment_familyhome_tl_tab);
        devicell = view
                .findViewById(R.id.fragment_familyhome_device_ll);
        alarmll = view
                .findViewById(R.id.fragment_familyhome_alarm_ll);
        healthll = view
                .findViewById(R.id.fragment_familyhome_health_ll);
        oldsterll = view
                .findViewById(R.id.fragment_familyhome_oldster_ll);
        bloodoxyTv = view
                .findViewById(R.id.item_viewpager_bloodoxy_tv);
        bloodpressTv = view
                .findViewById(R.id.item_viewpager_bloodpress_tv);
        bloodsugerTv = view
                .findViewById(R.id.item_viewpager_bloodsuger_tv);
        heartrateTv = view
                .findViewById(R.id.item_viewpager_heartrate_tv);
        huxiTv = view.findViewById(R.id.item_viewpager_huxi_tv);
        temperatureTv = view
                .findViewById(R.id.item_viewpager_temperature_tv);
        photoimageView = view
                .findViewById(R.id.item_viewpager_headphoto_iv);
        nameTv = view.findViewById(R.id.item_viewpager_name_tv);
        adddevice = view.findViewById(R.id.family_add_device_iv);
        addolder = view.findViewById(R.id.family_add_older_iv);
        devicell.setOnClickListener(new click());
        alarmll.setOnClickListener(new click());
        healthll.setOnClickListener(new click());
        oldsterll.setOnClickListener(new click());
        adddevice.setOnClickListener(new click());
        addolder.setOnClickListener(new click());
    }

    /**
     * 点击事件的监听 ClassName: click
     *
     * @author fzq
     * @Description: TODO
     * @date 2017-10-22
     */
    private class click implements OnClickListener {
        @Override
        public void onClick(View v) {
            Intent intent = null;
            switch (v.getId()) {
                case R.id.fragment_familyhome_device_ll:
                    intent = new Intent(getActivity(),
                            FamilyAllDeviceActivity.class);
                    startActivity(intent);
                    break;
                case R.id.fragment_familyhome_alarm_ll:
                    if (oldId != null) {
                        // 单个老人的报警
                        intent = new Intent(getActivity(),
                                FamilyAlarmListActivity.class);
                        intent.putExtra("olderId", "" + oldId);
                        intent.putExtra("oldName", "" + oldName);
                        intent.putExtra("oldpicId", "" + oldpicId);
                        startActivity(intent);
                    }
                    break;
                case R.id.fragment_familyhome_health_ll:
                    if (oldId != null) {

                        // 单个老人的健康数据
                        // 老人详情界面
                        intent = new Intent(getActivity(),
                                FamilyHealthListActivity.class);
                        intent.putExtra("olderId", "" + oldId);
                        startActivity(intent);
                    }
                    break;
                case R.id.fragment_familyhome_oldster_ll:
                    if (oldId != null) {
                        intent = new Intent(getActivity(),
                                FamilyOldrestDetailsActivity.class);
                        intent.putExtra("olderId", "" + oldId);
                        intent.putExtra("olderName", "" + oldName);
                        intent.putExtra("roomId", "" + roomId);
                        intent.putExtra("olderPhotopath", "" + olderPhotopath);
                        intent.putExtra("olderBir", "" + olderBir);
                        intent.putExtra("olderSex", "" + olderSex);
                        intent.putExtra("olderage", "" + olderage);
                        intent.putExtra("roombed", "" + roomBed);
                        intent.putExtra("bedNo", "" + bedNo);
                        intent.putExtra("olderEcgname", "" + olderEcgname);
                        intent.putExtra("olderEcgPhone", "" + olderEcgPhone);
                        startActivity(intent);
                    }
                    break;
                case R.id.family_add_device_iv:
                    intent = new Intent(getActivity(), CaptureActivity.class);
                    startActivity(intent);
                    break;
                case R.id.family_add_older_iv:
                    intent = new Intent(getActivity(), FamilyAddOlderActivity.class);
                    startActivity(intent);
                    break;
                default:
                    break;
            }
        }
    }

    // 交给presenter处理数据逻辑
    @Override
    public void initData() {
        super.initData();
        mPresenter.getOlderList(getActivity(), mTabLayout, bloodoxyTv,
                bloodpressTv, bloodsugerTv, heartrateTv, huxiTv, temperatureTv,
                photoimageView, nameTv);
    }

    @Override
    protected FragmentFamilyHomePresenter getPresenter() {
        return new FragmentFamilyHomePresenter(this);
    }

    /**
     * 回调老人的id
     */
    @Override
    public void sendId(String id, String name, String picpath, String bir,
                       String age, String roombed, String sex, String egname,
                       String egphone, String roomid, String bedno) {
        oldId = id;
        oldName = name;
        olderPhotopath = picpath;
        olderage = age;
        roomBed = roombed;
        olderBir = bir;
        olderSex = sex;
        olderEcgname = egname;
        olderEcgPhone = egphone;
        roomId = roomid;
        bedNo = bedno;
        Log.e("fzq", "" + olderSex + "   " + olderage + "  " + roomBed + "  " + olderBir);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            if ((Boolean) SPUtils.get(getActivity(), "addOldFlag", false)) {
                SPUtils.put(getActivity(), "addOldFlag", false);
                mPresenter.getOlderList(getActivity(), mTabLayout, bloodoxyTv,
                        bloodpressTv, bloodsugerTv, heartrateTv, huxiTv, temperatureTv,
                        photoimageView, nameTv);
            }
        }
    }
}
